﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace UFODefense
{
    class UFO : DrawableGameComponent
    {
        Rectangle m_location;   
        public Rectangle location {
            get { return m_location; }
        }
        Rectangle m_ground;
        Random m_randomizer;
        //help variables to calculate speed with fractions
        float m_Xlocation;
        float m_Ylocation;
        float m_speed;
        float m_Xspeed;
        float m_Yspeed;
        Vector2 m_target;
        Texture2D m_texture;
        SpriteBatch m_spriteBatch;
        ContentManager m_contentManager;

        public UFO(Game game, SpriteBatch SB, ContentManager CM, Rectangle ground, Random randomizer)
            : base(game)
        {
               
            m_spriteBatch = SB;
            m_contentManager = CM;
            m_ground = ground;
            m_randomizer = randomizer;

            game.Components.Add(this);
        }

        public override void Initialize()
        {

            int locationY = (int)Definitions.SCREEN_MIN_Y;
            int locationX = RandomNumber((int)Definitions.SCREEN_MIN_X, (int)Definitions.SCREEN_MAX_X);

            m_Xlocation = locationX;
            m_Ylocation = locationY;

            m_location = new Rectangle(locationX, locationY, Definitions.UFO_WIDTH, Definitions.UFO_HEIGHT);

            m_speed = Definitions.UFO_MIN_SPEED;
            NewTarget();

            base.Initialize();
        }

        protected override void LoadContent()
        {
            m_texture = m_contentManager.Load<Texture2D>("GFX/UFO1");

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            //PLACEHOLDER if the UFO touches the ground, make it destroy itself
            
            if (m_location.Intersects(m_ground))
            {
                Enabled = false;
                Visible = false;
            }
            
            //if we are in target, calculate new target;
            if (m_location.Contains((int)m_target.X, (int)m_target.Y))
            {
                NewTarget();
            }


            //see how the UFO should be moving towards it's target and adjust the location accordingly
            if (m_Xlocation > m_target.X) m_Xlocation -= Definitions.UFO_SPEED_FACTOR * m_Xspeed;
            else m_Xlocation += Definitions.UFO_SPEED_FACTOR * m_Xspeed;

            if (m_Ylocation > m_target.Y) m_Ylocation -= Definitions.UFO_SPEED_FACTOR * m_Xspeed;
            else m_Ylocation += Definitions.UFO_SPEED_FACTOR * m_Yspeed;

            float maxUFOX = (Definitions.SCREEN_MAX_X - Definitions.UFO_WIDTH);
            //check that we are not out of bounds on the X-axis, if we are set X to maxUfoX
            if (m_Xlocation > maxUFOX) m_Xlocation = maxUFOX;

            //at the end, cast the calculated new fractional locations into the m_location Rectangle int values
            m_location.X = (int)m_Xlocation;
            m_location.Y = (int)m_Ylocation;

            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            m_spriteBatch.Draw(m_texture, m_location, Color.White);
            base.Draw(gameTime);
        }

        private int RandomNumber(int min, int max)
        {
            return m_randomizer.Next(min, max);
        }
        /// <summary>
        /// Calculates the x-speed and y-speed with Pythagoras algorithm based on the desired total speed
        /// </summary>
        private void CalculateSpeeds()
        {
            //calculate the legs of the right triangle
            float Xdiff = Math.Abs(m_target.X - m_location.X);
            float Ydiff = Math.Abs(m_target.Y - m_location.Y);
            //calculate the hypothenusa
            float routeLength = (float)(Math.Sqrt((Xdiff * Xdiff) + (Ydiff * Ydiff)));

            //use the calculated lenghts to proportionize the x and y speeds
            m_Xspeed = (Xdiff / routeLength) * m_speed;

            m_Yspeed = (Ydiff / routeLength) * m_speed;

        }
        /// <summary>
        /// Called when the UFO has reached it's previous target (or is created) to calculate a new target
        /// </summary>
        private void NewTarget()
        {
            int targetY = RandomNumber(m_location.Y, (int)Definitions.SCREEN_MAX_Y);
            int targetX = RandomNumber((int)Definitions.SCREEN_MIN_X, (int)Definitions.SCREEN_MAX_X);
            m_target.X = (float)targetX;
            m_target.Y = (float)targetY;
            CalculateSpeeds();
        }
        public void Destroy()
        {
            Enabled = false;
            Visible = false;
        }

    }
}
